package com.joinway.framework.extension.console.cli.pdftk;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

public class PDFtkCommandTool {

	public static final class Operation {
		public static final String DumpData = "dump_data";
		public static final String DumpDataUtf8 = "dump_data_utf8"; 
		public static final String DumpDataFields = "dump_data_fields";
		public static final String DumpDataFieldsUtf8 = "dump_data_fields_utf8";
		public static final String DumpDataAnnots = "dump_data_annots";
	}
	
	/**
	 * 可执行文件路径
	 */
	private String path;
	
//	/**
//	 * 是否是windows系统
//	 */
//	private boolean win;
	
	private boolean debug = false;
	
	public PDFtkCommandTool(String path){
		this.path = path;
//		win = System.getProperty("os.name").toLowerCase().startsWith("win");
	}
	
	public List<String> getDumpData(String file) throws IOException, InterruptedException{
		return getDumpData(file, Operation.DumpDataUtf8);
	}
	
	public List<String> getDumpData(String file, String option) throws IOException, InterruptedException{
		List<String> lines = new ArrayList<>();
		
//		String cmd = MessageFormat.format("{0} \"{1}\" {2}", new Object[]{path, file, option});
		String cmd = MessageFormat.format("{0} {1} {2}", new Object[]{path, file, option});
		
		infoDebugger("execute command " + cmd);
		Process proc = Runtime.getRuntime().exec(cmd);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
		String line = null;
		while((line = reader.readLine()) != null){
			infoDebugger(line);
			lines.add(line);
		}
		
		reader.close();
		
		int code = proc.waitFor();
		if(code < 0){
			errorDebugger("failed to execute command " + cmd);
		}
		
		return lines;
	}

	public void split(String srcFile, String destFile, int fromPage, int toPage) throws IOException, InterruptedException{
//		String cmd = MessageFormat.format("{0} \"{1}\" cat {2}-{3} output {4}", new Object[]{path, srcFile, fromPage <= 0 ? 1 : fromPage, toPage, destFile});
		String cmd = MessageFormat.format("{0} {1} cat {2}-{3} output {4}", new Object[]{path, srcFile, fromPage <= 0 ? 1 : fromPage, toPage, destFile});
		infoDebugger("execute command " + cmd);
		
		Process proc = Runtime.getRuntime().exec(cmd);
		int code = proc.waitFor();
		
		if(code < 0){
			String errorMessage = IOUtils.toString(proc.getErrorStream());
			errorDebugger("failed to execute command " + cmd);
			errorDebugger(errorMessage);
		}
	}
	
	private void infoDebugger(String text){
		if(debug){
			System.out.println(getTimeStamp() + " " + text);
		}
	}
	
	private void errorDebugger(String text){
		if(debug){
			System.err.println(getTimeStamp() + " " + text);
		}
	}
	
	private String getTimeStamp(){
		return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss,SSS");
	}
	
	public boolean isDebug() {
		return debug;
	}

	public void setDebug(boolean debug) {
		this.debug = debug;
	}
}

